package com.sd.repay.service.impl;

import com.sd.repay.dao.BaseDao;
import com.sd.repay.pojo.ZqMerchant;
import com.sd.repay.service.ZqMerchantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 666666 on 2018/4/19.
 */
@Service
public class ZqMerchantServiceImpl implements ZqMerchantService {

    @Autowired
    private BaseDao baseDao;

    private static Logger log = LoggerFactory.getLogger(ZqMerchantServiceImpl.class);


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int saveZqMerchant(ZqMerchant zqMerchant) throws Exception {
        String sql = " INSERT INTO t_zq_merchant_info(report_no, merchant_no, acq_code, card_no, zq_merchant_no, zq_status, effective_status, resp_code, resp_msg, create_time, bak1)" +
                " VALUES(?,?,?,?,?,?,?,?,?,?,?) ";

        List<Object> params = new ArrayList<>();
        params.add(zqMerchant.getReportNo());
        params.add(zqMerchant.getMerchantNo());
        params.add(zqMerchant.getAcqCode());
        params.add(zqMerchant.getCardNo());
        params.add(zqMerchant.getZqMerchantNo());
        params.add(zqMerchant.getZqStatus());
        params.add(zqMerchant.getEffectiveStatus());
        params.add(zqMerchant.getRespCode());
        params.add(zqMerchant.getRespMsg());
        params.add(new Date());
        params.add(zqMerchant.getBak1());

        return baseDao.execute(sql, params.toArray());
    }

    @Override
    public ZqMerchant getByMerAndAcqAndCard(String merhcnatNo, String acqCode, String cardNo) throws Exception {
        String sql = " SELECT * FROM t_zq_merchant_info WHERE merchant_no = ? AND acq_code = ? AND card_no = ? ORDER BY id DESC";
        return baseDao.findFirst(ZqMerchant.class, sql, new Object[]{merhcnatNo, acqCode, cardNo});
    }

    @Override
    public ZqMerchant getByMerAndAcq(String merhcnatNo, String acqCode) throws Exception {
        String sql = " SELECT * FROM t_zq_merchant_info WHERE merchant_no = ? AND acq_code = ? ORDER BY id DESC";
        return baseDao.findFirst(ZqMerchant.class, sql, new Object[]{merhcnatNo, acqCode});
    }

    @Override
    public ZqMerchant getByReportNo(String reportNo) throws Exception {
        String sql = " SELECT * FROM t_zq_merchant_info WHERE report_no = ? ";
        return baseDao.findFirst(ZqMerchant.class, sql, reportNo);
    }
}
